<% if defined?(kv_tags) && kv_tags %>
  <% tiers = kv_tags.map(&:name).map { |tag| tag.split(":").first }.uniq.sort %>
  <% if @models
       tierunset = tiers.map { |tier|
         reg = ActiveRecord::Base.connection.quote("^" + tier + ":")
         regact = (ApplicationRecord.connection.adapter_name == "PostgreSQL") ? "~" : "REGEXP"
         [tier, @models.where("(select count(*) from tags join taggings on tags.id=taggings.tag_id where tags.name #{regact} #{reg} and taggings.taggable_id=models.id and taggings.taggable_type='Model')<1").count]
       }.to_h
     end %>
<% end %>
<%- tag_html_opts = {data: {bulk_item_tags: defined?(model_id) ? model_id&.to_s : nil}} %>
<%- heatmap = defined?(show_count) ? heatmap : tag_cloud_settings["heatmap"] %>
<%= render_component_collection(Components::Tag, :tag, tags, filters: filter.to_params.merge(@additional_filters), html_options: tag_html_opts, show_count: heatmap, filter_in_place: @filter_in_place) if tags %>
<% if defined?(kv_tags) && kv_tags %>
  <ul class="list-unstyled">
    <% tiers.each do |tier| %>
      <li>
        <details id="<%= tier %>" data-controller="tag-section" data-action="toggle->tag-section#saveState">
          <summary><%= tier %></summary>
          <%= render "tag_list", tags: kv_tags.select { |obj| obj.name.match?("^#{tier}:") }, filter: @filter %>
          <%- if tierunset && tierunset[tier] > 0 %>
            <%= link_to "unset (#{tierunset[tier]})", filter.to_params.merge(missingtag: tier), {class: "badge rounded-pill border border-muted text-danger tag"} %>
          <%- end %>
        </details>
      </li>
    <% end %>
  </ul>
<% end %>
<% if defined?(unrelated_tag_count) && unrelated_tag_count && unrelated_tag_count > 0 %>
  <p class="small"><%= t ".unrelated_tag_count", count: unrelated_tag_count %></p>
<% end %>
